﻿$(function () {
    Init_ProductQuery_dgv();
});
 
window.operateEvents = {
    'click #product_edit': function (e, value, row, index) {
        alert('You click like action, row: ' + JSON.stringify(row));
    },
    'click #product_remove': function (e, value, row, index) {
        $table.bootstrapTable('remove', {
            field: 'id',
            values: [row.id]
        });
    }
};
 
function ProductQuery_params(params) {
    var param = {
        PageNumber: params.pageNumber,
        PageSize: params.pageSize,
        Code: $("#Code").val(),
        Name: $("#Name").val(),
    };
    return param;
}

function Init_ProductQuery_dgv() {
    $('#table_dgv').bootstrapTable({
        url: "/products/product/Index",
        method: "post",
        cache: false,
        striped: true,
        pagination: true,
        sidePagination: 'server',
        toolbar: "#toolbar",
        showColumns:true,
        pageNumber: 1,
        pageSize: 10,
        pageList: [10, 25, 50, 100, 200],  
        queryParams: ProductQuery_params,
        columns: [
            { 
                checkbox: true, 
            },{
                field: 'ProductID',
                title: '产品ID',
                align: 'center',
                visible:'false'
               
            },{
                field: 'Code',
                title: 'SKU',
                align: 'center'
            },
            {
                field: 'Name',
                title: '产品名称',
                align: 'center'
            }, {
                field: 'ProductDesc',
                title: '产品说明',
                align: 'center'
            },  {
                field: 'ProductPackage',
                title: '产装说明',
                align: 'center'
            },{
                field: 'Cost',
                title: '成本',
                align: 'center'
            }, {
                field: 'Weight',
                title: '重量',
                align: 'center'
            }, {
                field: 'IsDel',
                title: '状态',
                align: 'center'
            }, {
                field: 'operate',
                title: '操作',
                align: 'center',
                events: operateEvents,
                formatter: operateFormatter
            }
        ],
        onLoadSuccess: onSuccess_Product,
        detailView:true,
        detailFormatter: ProductQuery_detailFormatter,
        //onExpandRow: ProductQuery_onExpandRow
    });
}

function operateFormatter(value, row, index) {
    return [
        '<a id="product_edit" href="javascript:void(0)" title="编辑">',
        '<i class="icon-edit"></i>',
        '</a>  ',
        '<a id="product_remove" href="javascript:void(0)" title="删除">',
        '<i class="icon-remove"></i>',
        '</a>'
    ].join('');
}

function ProductQuery_detailFormatter(i, row) {
   
    var $row1 = $("<div class='row-fluid'></div>");
    var $row2 = $("<div class='row-fluid'></div>");
    for (var i = 0; i < row.Images; i++) {
        var $item = $("<div class='span2'>" +
                    "<div class='item'>" +
                        "<a class='fancybox-button' data-rel='fancybox-button' title='Photo' href='media/image/image1.jpg'>" +
                            "<div class='zoom'>" +
                            "<img src='media/image/image1.jpg' alt='Photo' />" +
                                "<div class='zoom-icon'></div>" +
                            "</div>" +
                        "</a>" +
                        "<div class='details'>" +
                            "<a href='#' class='icon'><i class='icon-paper-clip'></i></a>" +
                            "<a href='#' class='icon'><i class='icon-link'></i></a>" +
                            "<a href='#' class='icon'><i class='icon-pencil'></i></a>" +
                            "<a href='#' class='icon'><i class='icon-remove'></i></a>" +
                        "</div>" +
                    "</div>" +
                 "</div>");
        if (i < 6) {
            $row1.append($item);
        } else {
            $row2.append($item);
        }
    }
    return $row1 + $row2; 
}

function ProductQuery_onExpandRow(index, row, $detail) {
    var s = row;
}

function onSuccess_Product(jsonData) {
    $.AjaxMsgData(jsonData, onSuccess_Product_Success);

}

function onSuccess_Product_Success(jsonData) {
    $('#table_dgv').bootstrapTable("load", jsonData.Data);
}

function AddProductSubmitSuccess(jsonData) {
    $.AjaxMsgData(jsonData);
}

